package com.byxy.mysystem.controller.testUser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.byxy.mysystem.model.testUser.TestCity;
import com.byxy.mysystem.model.testUser.TestCityCascaderDTO;
import com.byxy.mysystem.model.testUser.TestUser;
import com.byxy.mysystem.service.testUser.ITestCityService;
import com.byxy.mysystem.util.JsonResult;
import com.byxy.mysystem.util.ResultTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 详细地址表 前端控制器
 * </p>
 *
 * @author omd
 * @since 2023-02-28
 */
@RestController
@RequestMapping("/testCity")
public class TestCityController {

    @Autowired
    private ITestCityService testCityService;

    @PostMapping("/findTestCityCascader")
    public JsonResult view(@RequestBody TestCity testCity){
        //查询一级
        QueryWrapper<TestCity> wrapper = new QueryWrapper<>();
        wrapper.eq("type","1");
        List<TestCity> list = testCityService.list(wrapper);
        if(!CollectionUtils.isEmpty(list)){
            //查询二级
            for(TestCity item:list){
                QueryWrapper<TestCity> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("type","2");
                wrapper2.eq("pid",item.getId());
                List<TestCity> list2 = testCityService.list(wrapper2);
                if(!CollectionUtils.isEmpty(list2)){
                    // 查询三级
                    for(TestCity item2:list2){
                        QueryWrapper<TestCity> wrapper3 = new QueryWrapper<>();
                        wrapper3.eq("type","3");
                        wrapper3.eq("pid",item2.getId());
                        List<TestCity> list3 = testCityService.list(wrapper3);
                        if(!CollectionUtils.isEmpty(list3)){
                            item2.setChildren(list3);
                        }
                    }
                    item.setChildren(list2);
                }
            }
        }
        return ResultTool.success(TestCityCascaderDTO.buildList(list));
    }


}
